package com.why.service.Iml;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.why.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;

/**
 * @program: guli_parent
 * @description:
 * @author: @why
 * @create: 2021-02-15 17:14
 **/
@Service
public class MsmServiceImpl implements MsmService {

    private Boolean flag=false;
    /**
     * 发送短信的方法
     *
     * @param map：验证码
     * @param phoneNumber：手机号
     * @return
     */
    @Override
    public boolean sendSMS(HashMap<String, String> map, String phoneNumber) {
        if(!flag){
            return   sendSmsByEmaill(map,phoneNumber);
        }

        if (StringUtils.isEmpty(phoneNumber)){
            return false;
        }
        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF");
        IAcsClient client = new DefaultAcsClient(profile);
//        设置参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

//        设置发送相关的参数
//        设置发送的手机号
        request.putQueryParameter("PhoneNumbers", phoneNumber);
//        设置签名名称，申请的时候使用的名称
        request.putQueryParameter("SignName", "GL在线教育验证服务");
//      阿里云模板的code
        request.putQueryParameter("TemplateCode", "SMS_190277104");
//       发送过去的验证码
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
//            返回成功
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }



    @Autowired
    JavaMailSenderImpl sender;

    /**
     * 发送邮箱验证码
     * @param map
     *
     * @return
     */

    private boolean sendSmsByEmaill(HashMap<String, String> map, String emial) {
        SimpleMailMessage message = new SimpleMailMessage();
        System.out.println(emial);
        message.setSubject("申请注册的验证码为...");
        String code = map.get("code");
        code="【"+code+"】";
        message.setText("申请注册的验证码为"+code+",5分钟内有效!");
        System.out.println(code);
        message.setFrom("1585049082@qq.com");
        message.setTo(emial);
        sender.send(message);
        return true;

    }
}
